package com.china.produce.config.datasource;

import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import com.china.produce.dynamic.DynamicMybatisPlugin;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.env.Environment;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import javax.sql.DataSource;

/**
 *
 *
 * @ClassName
 * @Author niujie
 * @Version
 * @Description
 * @CreateTime 2023/2/7
 */
@Configuration
@MapperScan(basePackages = {"com.china.produce.mapper.study"}, sqlSessionFactoryRef = "userReadSessionFactory")
public class StudyDataSource implements EnvironmentAware{

    private static String MAPPER_READ_LOCATION = "";

    @Bean(name = "userReadDataSource")
    @ConfigurationProperties("spring.datasource.druid.study")
    public DataSource userReadDataSource() {
        return DruidDataSourceBuilder.create().build();
    }

    @Bean(name = "userReadSessionFactory")
    public SqlSessionFactory userReadSessionFactory(@Qualifier("userReadDataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(MAPPER_READ_LOCATION));
        bean.setPlugins(new Interceptor[]{
                plugin()
        });
        org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
        configuration.setMapUnderscoreToCamelCase(true);
        bean.setConfiguration(configuration);

        return bean.getObject();
    }

    @Bean(name = "userReadTransactionManager")
    public DataSourceTransactionManager userReadTransactionManager(@Qualifier("userReadDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

//    @Bean(name = "userReadSessionTemplate")
//    public SqlSessionTemplate userReadSessionTemplate(@Qualifier("userReadSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
//        return new SqlSessionTemplate(sqlSessionFactory);
//    }

    //创建study库的数据源的事务管理器，否则在需要发生回滚时@Transactional会报错
    @Bean(name = "userReadManager")
    @Primary
    public PlatformTransactionManager txManager(@Qualifier("userReadDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Override
    public void setEnvironment(Environment environment) {

        MAPPER_READ_LOCATION = environment.getProperty("mybatis.study.mapper-locations");
    }

    @Bean
    public Interceptor plugin() {

        return new DynamicMybatisPlugin();
    }
}
